$(function() {
    $(".btn-submit-login").click(function() {
        if (typeof(Storage) !== "undefined")
        {
            if (checkValidate_signup('error-msg')) {
                $(".btn-submit-login").attr("disabled", "disabled");
                $.post(localStorage.domain_app+"/webservice/facebook/signupFacebook.php", {
                    fianceFirstName: $('input[name="fiance_firstname"]').val(),
                    fianceLastName: $('input[name="fiance_lastname"]').val(),
                    weddingDate: $('input[name="fiance_date"]').val(),
                    user_id: localStorage.userID

                }, function(res) {
                    var msgEr = '';
                    var num_milliseconds = 3000;
                    if (res.error == 1) {
                        msgEr = res.msg;
                        $('#error-msg').addClass('active').children().html(msgEr);
                        hideError_2('error-msg', num_milliseconds);

                    } else {
                        wd_redirect('home.html');
                    }
                    return false;

                    $(".btn-submit-login").removeAttr("disabled");
                }, "json");
            }
        } else {
            $('#error-msg').addClass('active').children().html("Update information was failed.");
            hideError_2('error-msg', 3000);
            return false;
        }
    });
    $('input[type="text"]').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");

        } else if (this.value != this.defaultValue) {
            
            this.select();
        }

    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
            
        }
    });
    $('input[type="date"]').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");

        } else if (this.value != this.defaultValue) {
           
            this.select();
        }

    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
            
        }
    });
});
function checkValidate_signup(id_append) {
    var check_char = /^[a-zA-Z\s]+$/;
    // error msg depends on the language
    var errormsg = new Array();
    errormsg[1] = 'The %s field is required.';
    errormsg[2] = 'Please input your %s';
    errormsg[3] = 'The %s field must contain only numbers.';
    errormsg[4] = 'This %s address is invalid.';
    errormsg[5] = '%s is too short. %s2 must be at least %number characters';
    errormsg[6] = 'Confirm password does not match password';
    errormsg[7] = 'The %s field must not exceed %number characters in length.';
    errormsg[8] = 'This %s is invalid.';
    //show msg
    var msgEr = '';
    var valueStr = '';
    var num_milliseconds = 3000;
    if ($('input[name="fiance_firstname"]').val() == '' || $('input[name="fiance_firstname"]').val() == 'Fiance’s First Name') {
        msgEr = errormsg[2].replace('%s', 'fiance’s first name');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="fiance_firstname"]'), num_milliseconds);
        return false;
    } else {
        valueStr = $('input[name="fiance_firstname"]').val();
        if (!check_char.test(valueStr)) {
            $('#' + id_append + '').addClass('active').children().html("Fiance’s first name cannot include special characters");
            hideError(id_append, $('input[name="fiance_firstname"]'), num_milliseconds);
            return false;
        } 

    }

    if ($('input[name="fiance_lastname"]').val() == '' || $('input[name="fiance_lastname"]').val() == 'Fiance’s Last Name') {
        msgEr = errormsg[2].replace('%s', 'fiance’s last name');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="fiance_lastname"]'), num_milliseconds);
        return false;
    }
    else {
        valueStr = $('input[name="fiance_lastname"]').val();
        if (!check_char.test(valueStr)) {
            $('#' + id_append + '').addClass('active').children().html("Fiance’s last name cannot include special characters");
            hideError(id_append, $('input[name="fiance_lastname"]'), num_milliseconds);
            return false;
        } 

    }
    if ($('input[name="fiance_date"]').val() == '' || $('input[name="fiance_date"]').val() == 'Wedding Date (mm/dd/yy)') {
        msgEr = errormsg[2].replace('%s', 'wedding date');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="fiance_date"]'), num_milliseconds);
        return false;
    }
    else {
        var rxDatePattern = /(19|20)\d\d[- \/.](0?[1-9]|1[012])[- \/.](0?[1-9]|[12][0-9]|3[01])/;
        var date = $('input[name="fiance_date"]').val();

        if (!date.match(rxDatePattern)) {
            msgEr = errormsg[8].replace('%s', 'wedding date');
            $('#' + id_append + '').addClass('active').children().html(msgEr);
            hideError(id_append, $('input[name="fiance_date"]'), num_milliseconds);
            return false;
        } 
    }
    if (!$('input[name="fiance_condition"]').is(':checked')) {
        msgEr = "Please agree the Terms and Conditions of this application.";
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="fiance_condition"]'), num_milliseconds);
        return false;
    }
    return true;
}
function hideError(id_append, item, num_milliseconds) {
    item.css("color", "red");
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function hideError_2(id_append,num_milliseconds) {
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}